package com.serviceprovider.business.module.order.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.order.model.OrderStatus;
import com.serviceprovider.business.module.order.model.OrderStatusForm;
import com.serviceprovider.business.module.order.services.interfaces.IOrderStatusService;
import com.serviceprovider.business.module.order.valueobjects.OrderStatusVO;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

@Controller
@RequestMapping("/admin/order_status.htm")
public class EditOrderStatusController {
    @Autowired
    private IOrderStatusService orderStatusService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView viewOrderStatuses() {
        ModelAndView      result = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDER_STATUS_LIST);
        List<OrderStatus> list   = orderStatusService.findAll();
        OrderStatusForm   form   = new OrderStatusForm();

        for (OrderStatus status : list) {
            form.getOrderStatusList().add(new OrderStatusVO(status));
        }

        result.addObject("orderStatusForm", form);

        return result;
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "add"
    )
    public ModelAndView addNewOrderStatus(@ModelAttribute("orderStatusForm") OrderStatusForm form) {
        ModelAndView result = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDER_STATUS_LIST);

        orderStatusService.create(new OrderStatus(form.getNewOrderStatus()));

        List<OrderStatus> list = orderStatusService.findAll();

        for (OrderStatus status : list) {
            form.getOrderStatusList().add(new OrderStatusVO(status));
        }

        result.addObject("orderStatusForm", form);

        return result;
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "remove"
    )
    public ModelAndView removeOrderStatus(@ModelAttribute("orderStatusForm") OrderStatusForm form) {
        ModelAndView result = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDER_STATUS_LIST);

        for (OrderStatusVO status : form.getCheckedStatuses()) {
            orderStatusService.remove(status.getId());
        }

        List<OrderStatus> list = orderStatusService.findAll();

        for (OrderStatus status : list) {
            form.getOrderStatusList().add(new OrderStatusVO(status));
        }

        result.addObject("orderStatusForm", form);

        return result;
    }

    @RequestMapping(
        method = RequestMethod.POST,
        params = "save"
    )
    public ModelAndView saveOrderStatus(@ModelAttribute("orderStatusForm") OrderStatusForm form) {
        ModelAndView result = new ModelAndView(RequestConstants.OrderRequests.VIEW_ORDER_STATUS_LIST);

        for (OrderStatusVO status : form.getCheckedStatuses()) {
            orderStatusService.update(new OrderStatus(status));
        }

        List<OrderStatus> list = orderStatusService.findAll();

        for (OrderStatus status : list) {
            form.getOrderStatusList().add(new OrderStatusVO(status));
        }

        result.addObject("orderStatusForm", form);

        return result;
    }
}



